/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */
#include "tsiniter.h"
#include <QApplication>


TsIniter::TsIniter(const QString & user, QObject *parent) :
    QObject(parent)
{
    this->username=user;
    this->qstate=false;
    this->wstate=false;
}

void TsIniter::slotDb(const QString &plName, int id,QSqlQuery *ret){

    if(plName=="core"){
        this->id=id;
        // наш клиент, обрабатываем данные
        emit this->sigPutLog(PLUGIN_NAME,3,"Debug: database return data ");
        while(ret->next()){
                QString on1,on2;
                on1=ret->value(0).toString();
                on2=ret->value(1).toString();
                emit this->sigClear(this->id);
                emit this->sigPutLog(PLUGIN_NAME,3,"Debug: data plugin name <"+
                                     on1+"> config string <"+
                                     on2+">");
                QStringList aa;
                aa.append(on1);
                aa.append(on2);
                data.append(aa);
                this->qstate=false;
                this->wstate=true;

        }
        // устанавлиаем состояние запроса в труЪ
        this->qstate=true;
        this->wstate=true;
    }

}

void TsIniter::slotDbErr(const QString &plName, const QString &errtext){
    // проверяем наша ли обшибка
    if(plName=="core"){
        emit this->sigPutLog(PLUGIN_NAME,2,"Debug: error query ");
        this->qstate=false;
        this->wstate=true;
    }
}

QList<QStringList> TsIniter::getList(){
    return this->data;
}

bool TsIniter::qstart(){

    QList<QVariant> aa;
    aa.append(this->username);
    emit this->sigQexec(PLUGIN_NAME,"get_user_plugins",aa);
    // вечный цикл с проверкой состояния что же вернется от плагина баз данных
    while(true){
        emit this->sigPutLog(PLUGIN_NAME,3,"Debug: sleep ... ");
        QApplication::processEvents();
        if(this->wstate){
            // состояние изменилось. выходим из сна
            //qDebug()<<"BR";
            emit this->sigPutLog(PLUGIN_NAME,3,"Debug: wake up ");
            break;
        }
    }
    return this->qstate;
}
